package com.taotao.merchant.aop;


import com.taotao.merchant.annotation.AutoIdempotent;
import com.taotao.merchant.service.TokenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.lang.reflect.Method;

@Aspect
@Component
public class IdempotentAspect {

    @Resource
    private TokenService tokenService;

    @Autowired
    private HttpServletRequest request;

    @Pointcut("@annotation(com.taotao.merchant.annotation.AutoIdempotent)")
    public void idempotent() {
    }

    @Around("idempotent()")
    public Object methodAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        AutoIdempotent idempotent = method.getAnnotation(AutoIdempotent.class);

        String token =request.getHeader("token");



        if (tokenService.delete(token)) {
            return joinPoint.proceed();
        }
        return "重复请求";
    }
}

